package com.longrise.android.byjk.plugins.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.Control;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.plugins.main.MainActivity;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.datasource.remote.ReloginBean;
import com.longrise.common.utils.DZZWTools;
import com.longrise.common.utils.NetUtil;


/**
 * Created by callxkj on 16/5/30.
 * 软件升级或者新安装 引导页
 */
public class GuideActivity extends Activity implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private ViewPager mVp;
    private Button mBt;
    private EntityBean fbean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        mVp = (ViewPager) findViewById(R.id.guide_vp);
        mBt = (Button) findViewById(R.id.guide_bt);
    }


    private void initData() {
        GuideAdapter mAdapter = new GuideAdapter();
        mVp.setAdapter(mAdapter);
    }

    private void initEvent() {
        mVp.addOnPageChangeListener(this);
        mBt.setOnClickListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }


    @Override
    public void onPageSelected(int position) {
        if (position == 2) {
            mBt.setVisibility(View.VISIBLE);
        }else{
            mBt.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.guide_bt:
                guideOver();
                Control.setGuideView(false);
                break;
        }
    }

    /**
     * 用户点击确认
     */
    public void guideOver(){
        boolean netWorkEnable = NetUtil.checkNetEnable();
        if (netWorkEnable) {
            toLogin();
        } else {
            loadLogin();
        }
    }

    private void toLogin() {
        long currentTime = System.currentTimeMillis();
        ReloginBean reloginBean = LoadDataManager.getInstance().getReloginBean();
        if (reloginBean != null) {
            long firstTime = reloginBean.getTime();
            long time = currentTime - firstTime;
            //604800000 一周
            if (time != 0 && time < 604800000) {
                reloginBean.setServiceName("bbt_login_sUserToken");
                reloginBean.setServiceUrl(UrlConstants.BaseUrl);
                LoadDataManager.getInstance().setReloginBean(reloginBean);
                login(reloginBean);
            } else {
                loadLogin();
            }
        } else {
            loadLogin();
        }
    }

    /**
     * 自动登录
     */
    private void login(ReloginBean requsetInfo) {
        fbean = requsetInfo.getBean();
        UserController.getInstance().login(UserController.GUIDE,this, fbean, new UserCallback() {
            @Override
            public void onSuccess(int status,String des) {
                if(status == UserCallback.HOME_PAGE){
                    loadMain();
                }
                //自动登录取消提示
                // DZZWTools.showToast(LaunchActivity.this, des, 2 * 1000);
            }

            @Override
            public void onFail(String des) {
                DZZWTools.showToast(GuideActivity.this, des, 2 * 1000);
                loadLogin();
            }

            @Override
            public void onFinish() {
                DZZWTools.dismissDialog(null);
            }
        });
    }

    /**
     * 去登陆界面
     */
    private void loadLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        GuideActivity.this.finish();
    }


    /**
     * 直接去主界面
     */
    private void loadMain() {
        GuideActivity.this.startActivity(new Intent(GuideActivity.this, MainActivity.class));
        GuideActivity.this.finish();
    }


    class GuideAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView iv = new ImageView(GuideActivity.this);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            switch (position){
                case 0:
                    iv.setImageResource(R.drawable.guide_pageone);
                    break;

                case 1:
                    iv.setImageResource(R.drawable.guide_pagetwo);
                    break;

                case 2:
                    iv.setImageResource(R.drawable.guide_pagethree);
                    break;

                default:
                    break;
            }
            container.addView(iv);
            return iv;
        }
    }
}
